package main
import "fmt"
type LNode struct {
	Val int
	Next *LNode
}
func createList(nums []int) *LNode {
	if len(nums) == 0 {
		return nil
	}
	head := &LNode{nums[0], nil}
	curr := head
	for i := 1; i < len(nums); i++ {
		curr.Next = &LNode{nums[i], nil}
		curr = curr.Next
	}
	return head
}
func printList(head *LNode) {
	for head != nil {
		fmt.Printf("%d ", head.Val)
		head = head.Next
	}
	fmt.Println()
}
func reverseList(head *LNode) *LNode {
	//prev当前节点的前一个节点，即反转之后当前结点的下一个结点，初始为nil
	var prev *LNode
	//curr当前结点
	curr := head
	for curr != nil {
		//next当前结点的下一个节点
		next := curr.Next
		//当前结点的next指向前一个结点
		curr.Next = prev
		prev = curr
		//当前结点后移
		curr = next
	}
	return prev
}
func main() {
	nums := []int{1, 2, 3, 4, 5}
	head := createList(nums)
	fmt.Println("原始链表:")
	printList(head)
	head = reverseList(head)
	fmt.Println("反转之后:")
	printList(head)
}